package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.TotalEvaluationAdapter;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshBase;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.AllEvaluationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/8/21.
 * 全部评价
 */

public class AllEvaluationActivity extends Activity implements OnClickListener , Callback.CommonCallback<String>{
    @Bind(R.id.retur)//返回
    LinearLayout retur;

    @Bind(R.id.total_evaluation)//总评论
            TextView total_evaluation;

    @Bind(R.id.rating)//评星
            RatingBar rating;

    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;
    ListView listview;
    @Bind(R.id.release_listview)
    PullToRefreshListView releaseListview;


    List<AllEvaluationModel> allEvaluationComment;
    TotalEvaluationAdapter adapter;

    Context context;

String pos="0";//0、商家中心
    String merchantid="0";
    String merchanttype="0";
    private MyProgressDialog mDialog;
    String Judge_interface = "0"; //获取数据

    // 请求数据
    private int currentPage = 1;

    private boolean mIsStart = true;//判断是否上下加载 true:下拉刷新  false:上拉加载数据
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_evaluation);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);

        listview=releaseListview.getRefreshableView();
        // 显示头部出现分割线
        listview.setHeaderDividersEnabled(false);
        // 禁止底部出现分割线
        listview.setFooterDividersEnabled(false);
        listview.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listview.setDividerHeight(0);
        releaseListview.setOnRefreshListener(refreshListener);
        adapter = new TotalEvaluationAdapter(context);
        listview.setAdapter(adapter);
        if (getIntent()!=null){
            pos=getIntent().getStringExtra("pos");
            merchantid=getIntent().getStringExtra("merchantid");
            merchanttype=getIntent().getStringExtra("merchanttype");
        }
        processautomaticorder();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }
    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        releaseListview.setLastUpdatedLabel(text);
    }
    /**
     * 上拉刷新，下拉获取更多
     */
    PullToRefreshBase.OnRefreshListener refreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            mIsStart = true;
            currentPage = 1;
            if (allEvaluationComment!=null){
                allEvaluationComment.clear();
            }
            if (adapter!=null){
                adapter.emptying();
            }
            processautomaticorder();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            mIsStart = false;
            currentPage++;
            processautomaticorder();
        }
    };
    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }
        SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return mDateFormat.format(new Date(time));
    }
    //获取数据
    private void processautomaticorder() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "0";
            String Data= Constant.MERCHANTCOMMENTCLEANING;//干洗
            if (merchanttype.equals("1")) {//房产商家
                Data= Constant.MERCHANTCOMMENTPURCHASE;

            } else if (merchanttype.equals("2")) {//购车商家
                Data= Constant.MERCHANTCOMMENTBUYACAR;

            } else if (merchanttype.equals("3")) {//干洗商家
                Data= Constant.MERCHANTCOMMENTCLEANING;

            } else if (merchanttype.equals("4")) {//开锁商家
                Data= Constant.MERCHANTCOMMENTUNLOCKING;

            } else if (merchanttype.equals("5")) {//果蔬商家
                Data= Constant.MERCHANTCOMMENTVEGETABLES;

            } else if (merchanttype.equals("6")) {//苦力商家
                Data= Constant.MERCHANTCOMMENTCOOLIE;

            } else if (merchanttype.equals("7")) {//快递商家

            } else if (merchanttype.equals("8")) {//家政商家
                Data= Constant.MERCHANTCOMMENTHOUSEKEEPING;

            } else if (merchanttype.equals("9")) {//洗车商家
                Data= Constant.MERCHANTCOMMENTCARWASH;
            }
            HttpClient.cleaning(Data,merchantid, currentPage+"", this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<MerchantsTypeModel> getnote = JsonTools.getmerchanttype(result);
        if (getnote != null || getnote.size() > 0) {
            String error = getnote.get(0).getError();
            String mssage = getnote.get(0).getMssage();
                if (error.equals("200")) {
                    available_layout.setVisibility(View.GONE);
                    String content = getnote.get(0).getResult();
                    List<AllEvaluationModel> allEvaluation = JsonTools.getAllEvaluation(content);
                    if (allEvaluation != null || allEvaluation.size() > 0) {
                        String comment = allEvaluation.get(0).getComment();
                        allEvaluationComment = JsonTools.getAllEvaluationComment(comment);
                        for(int i=0;i<allEvaluationComment.size();i++){//存入数据
                            adapter.addItem(allEvaluationComment.get(i));
                        }
                        if (adapter.number()>7){//大于7条时可以上拉加载
                            releaseListview.setPullLoadEnabled(false);
                            releaseListview.setScrollLoadEnabled(true);
                       }
                        List<AllEvaluationModel> allEvaluationCommentcount = JsonTools.getAllEvaluationCommentcount(allEvaluation.get(0).getCommentcount());
                        if (allEvaluationCommentcount != null || allEvaluationCommentcount.size() > 0) {
                            total_evaluation.setText("总评价："+allEvaluationCommentcount.get(0).getCount());
                            if (!Sundry.getStinges(allEvaluationCommentcount.get(0).getEvaluate())){
                                rating.setStar(Float.valueOf(allEvaluationCommentcount.get(0).getEvaluate()));//设置显示的星星个数
                            }else {
                                rating.setStar(0f);//设置显示的星星个数
                            }
                        }
                    }
                }else if (error.equals("105")) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivity(intent);
                    ToastUtils.show(context, mssage);
                } else if (error.equals("100")) {//没有数据
                    available_layout.setVisibility(View.VISIBLE);
                    ToastUtils.show(context, mssage);
                } else if (error.equals("150")) {//没有更多数据
                    ToastUtils.show(context, mssage);
                    available_layout.setVisibility(View.GONE);
                }else {
                    available_layout.setVisibility(View.VISIBLE);
                    ToastUtils.show(context, mssage);
                }
            }
        releaseListview.onPullDownRefreshComplete();
        releaseListview.onPullUpRefreshComplete();
        setLastUpdateTime();
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

            if (mDialog != null) {
                mDialog.dismiss();
            }
        releaseListview.onPullDownRefreshComplete();
        releaseListview.onPullUpRefreshComplete();
        setLastUpdateTime();
        available_layout.setVisibility(View.VISIBLE);
            ToastUtils.Toastshow(context, getResources().getString(R.string.server));


    }

    @Override
    public void onCancelled(CancelledException cex) {

            if (mDialog != null) {
                mDialog.dismiss();
            }

            releaseListview.onPullDownRefreshComplete();
             releaseListview.onPullUpRefreshComplete();
            setLastUpdateTime();
            available_layout.setVisibility(View.VISIBLE);

            ToastUtils.Toastshow(context, getResources().getString(R.string.server));


    }

    @Override
    public void onFinished() {

    }
}

